home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 313 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.0 KB

  1. Path: centre.univ-orleans.fr!desiree!emmguyot
  2. From: emmguyot@desiree.univ-orleans.fr (Emmanuel GUYOT)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Loading a structure from a file
  5. Date: 4 Jan 1996 09:38:07 GMT
  6. Organization: CITU - Universite d'Orleans - FRANCE
  7. Message-ID: <4cg75v$l2j@centre.univ-orleans.fr>
  8. References: <4cfg1d$5n9@vector.wantree.com.au>
  9. NNTP-Posting-Host: desiree.cnrs-orleans.fr
  10. X-Newsreader: TIN [version 1.2 PL2]
  11.  
  12. Jody Petroni (jpet@wantree.com.au) wrote:
  13. : Im trying to load this large structure from a file=>
  14.  
  15. : /*Existing Job Record*/
  16. : struct jobrec{
  17. :  int job;
  18. :  char desc[81];
  19. :  int amt[50];
  20. :  };struct jobrec jobrecord[100];
  21.  
  22. : rather than the following code=>
  23.  
  24. : while(fscanf(fleptr,"%d%s%d%d%d...(50 times)",jobrecord[x].job,
  25. :             jobrecord[x].desc,jobrecord[x].amt[y]..(50 times)..)!=EOF){
  26. : /*... Other code here*/
  27. : }
  28. : Is there an easier way to do this
  29.  
  30. If you don't bother about data portability, you can do this :
  31.  
  32. To write the structure :
  33.  
  34. fwrite (&(jobrecord[x]), sizeof(struct jobrec), 1, fleptr);
  35.  
  36. To read it :
  37.  
  38. fread (&(jobrecord[x]), sizeof(struct jobrec), 1, fleptr);
  39.  
  40. This works fine. The only problem is that depending on the compiler
  41. and on the computer, some padding bytes may have been added.
  42. Anyway, if you write and read the data on the same computer
  43. and both program where compiled with the same compiler,
  44. there won't be any problem.
  45.  
  46. E.G.
  47.  
  48. ------------------------------------------------------------------------------
  49. ---------------------------->>>> Emmanuel Guyot <<<<--------------------------
  50. LPCE-CNRS                              | 
  51. 3A avenue de la Recherche Scientifique | Phone     : 33 38 51 78 25
  52. 45071 Orleans Cedex 2                  | Fax       : 33 38 63 12 34
  53. France                                 | EMail     : emmguyot@cnrs-orleans.fr
  54. ------------------------------------------------------------------------------
  55. Home Page : http://desiree.cnrs-orleans.fr/cgi-bin/cpt_html?index
  56. ------------------------------------------------------------------------------
  57.